#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
一个简单带界面的检测应用.

author: Xie Junming
Last edited: 2022/11/07
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QDesktopWidget, QMainWindow, QAction, qApp
from PyQt5.QtCore import QPoint, pyqtSignal, QThread
from PyQt5.QtGui import QIcon
import cv2
from detection import Detection
import time


class mainWindows(QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()

        self.work = WorkThread()

    def initUI(self):
        self.setWindowTitle('Opencv')
        self.resize(600, 600)
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.center() + QPoint(-300, -300))

        self.statusBar().showMessage('Ready')

    def mainRun(self):
        btn_widget = QWidget(self)
        start_btn = QPushButton(btn_widget)
        start_btn.setText("启动")
        start_btn.clicked.connect(self.start_btn_slot)
        btn_widget.resize(100, 100)
        btn_widget.move(200, 200)
        self.show()

    def start_btn_slot(self):
        print("启动按钮")
        self.work.start()


class WorkThread(QThread):
    # 自定义信号对象。参数str就代表这个信号可以传一个字符串
    trigger = pyqtSignal(str)

    def __int__(self):
        # 初始化函数
        super(WorkThread, self).__init__()

    def run(self):
        # 重写线程执行的run函数
        print("开始检测")
        det = Detection()
        fig_name = "./img/test.jpg"
        det.read_img(fig_name)
        det.run()
        det.show_fig()
        det.savefig("./img/test_output.png")
        print("检测完成")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = mainWindows()
    ex.mainRun()
    sys.exit(app.exec_())
